package io.renren.authen.controller;

import io.renren.entity.TokenEntity;
import io.renren.entity.UserEntity;
import io.renren.service.TokenService;
import io.renren.service.WechatTokenService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/wechat")
public class WechatController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WechatTokenService WechatTokenService;

    @RequestMapping("/authorize")
    public String authorize(HttpServletRequest request, @RequestParam("returnUrl") String returnUrl) {
        String url = returnUrl + "/gaokao-api/wechat/userInfo";
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
        return "redirect:" + redirectUrl;
    }

    @RequestMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                           @RequestParam("state") String returnUrl, HttpServletRequest request,HttpServletResponse response) throws Exception {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        WxMpUser wxMpUser = new WxMpUser();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
            wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);

        } catch (WxErrorException e) {
            System.out.println("网页授权失败");
            return "redirect:http://www.baidu.com";
        }
        //验证OpenId
        String openId = wxMpUser.getOpenId();
        String headImg = wxMpUser.getHeadImgUrl();
        System.out.println("openId:" + openId);
        Map<String,String> newUser = new HashMap<>();
        newUser.put("openId",openId);
        newUser.put("headImg",headImg);
        UserEntity user = WechatTokenService.getUserByopenId(openId);
        if(user == null){
            WechatTokenService.createUser(newUser);
            user = WechatTokenService.getUserByopenId(openId);
        }
        TokenEntity tokenEntity = tokenService.createToken1(user);
        String token = tokenEntity.getToken();
        // String name=wxMpUser.getNickname();
        // String url=returnUrl+"/#/demo?headSrc="+wxMpUser.getHeadImgUrl()+"&choiceName="+URLEncoder.encode(name,"UTF-8");
      //  request.setAttribute(token, token);
        String url = returnUrl + "/#/demo?token=" + token;
        return "redirect:" + url;

    }
}
